﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;

namespace SharePoint.UserAccountControl.Web
{
    /// <summary>
    /// A custom admin page for checking user access.
    /// </summary>
    public class UserAccess : Page
    {
        protected System.Web.UI.HtmlControls.HtmlForm form1;
        protected Button checkAccessButton;
        protected SPGridView gridView;
        protected TextBox userNameTextBox;
        protected ContentPlaceHolder placeHolderMainContent;
        protected Label errorLabel;

        /// <summary>
        /// Initializes a new instance of the <see cref="UserAccess"/> class.
        /// </summary>
        public UserAccess()
        {
            Load += UserAccess_Load;
        }

        /// <summary>
        /// Handles the Load event of the UserAccess control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void UserAccess_Load(object sender, EventArgs e)
        {
            checkAccessButton.Click += checkAccessButton_Click;

            if (!Page.IsPostBack)
            {
                var colName = new BoundField
                {
                    DataField = "Description",
                    HeaderText = "Description",
                    SortExpression = "Description"
                };
                gridView.Columns.Add(colName);

            }
        }

        /// <summary>
        /// Handles the Click event of the checkAccessButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void checkAccessButton_Click(object sender, EventArgs e)
        {
            try
            {
                errorLabel.Visible = false;
                gridView.Visible = true;

                SPSecurity.RunWithElevatedPrivileges(() =>
                                                         {
                                                             var access = new SharePointAccess();
                                                             var result = access.CheckFarm(userNameTextBox.Text);

                                                             gridView.DataSource = result;
                                                             gridView.DataBind();
                                                         }
                    );
            }
            catch(Exception ex)
            {
                gridView.Visible = false;
                errorLabel.Text = ex.Message;
                errorLabel.Visible = true;
            }
        }
    }
}